﻿using System;
using System.Windows;
using eLearning.Util;
using Microsoft.Phone.Controls;
using eLearning.Model;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;

namespace eLearning.DosenView
{
    public partial class DashboardPage : PhoneApplicationPage
    {
        private Menu menu1;
        private Menu menu2;
        private Menu menu3;
        private Menu menu4;
        private Menu menu5;
        private Menu menuItem;
        private ObservableCollection<Menu> hiddenMenu;

        public DashboardPage()
        {
            InitializeComponent();
            InitializeMenu();
            InitializeHiddenMenu();
            CheckLoggedUser();
        }

        private void InitializeMenu()
        {
            Dictionary<string, Menu> menus = Setting.Instance.LoadMenuSetting();
            menu1 = menus["Menu1"];
            imgMenu1.Source = new BitmapImage(new Uri(menu1.Icon, UriKind.RelativeOrAbsolute)); ;
            textBlockMenu1.Text = menu1.Nama;
            menu2 = menus["Menu2"];
            imgMenu2.Source = new BitmapImage(new Uri(menu2.Icon, UriKind.RelativeOrAbsolute)); ;
            textBlockMenu2.Text = menu2.Nama;
            menu3 = menus["Menu3"];
            imgMenu3.Source = new BitmapImage(new Uri(menu3.Icon, UriKind.RelativeOrAbsolute)); ;
            textBlockMenu3.Text = menu3.Nama;
            menu4 = menus["Menu4"];
            imgMenu4.Source = new BitmapImage(new Uri(menu4.Icon, UriKind.RelativeOrAbsolute)); ;
            textBlockMenu4.Text = menu4.Nama;
            menu5 = menus["Menu5"];
            imgMenu5.Source = new BitmapImage(new Uri(menu5.Icon, UriKind.RelativeOrAbsolute)); ;
            textBlockMenu5.Text = menu5.Nama;
        }

        private void InitializeHiddenMenu()
        {
            ApplicationBar.MenuItems.Clear();
            hiddenMenu = Setting.Instance.HiddenMenu();
            for (int i = 0; i < hiddenMenu.Count; i++)
            {
                ApplicationBarMenuItem menu = new ApplicationBarMenuItem(hiddenMenu[i].Nama);
                menu.Click += new EventHandler(menu_Click);
                ApplicationBar.MenuItems.Add(menu);
            }
        }

        private void menu_Click(object sender, EventArgs e)
        {
            foreach(Menu menu in hiddenMenu){
                if (((ApplicationBarMenuItem)sender).Text.Equals(menu.Nama))
                    menuItem = menu;
            }
            if(menuItem != null)
                Utility.GotoPage(this, menuItem.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
            {
                InitializeMenu();
                InitializeHiddenMenu();
            }
        }

        private void ConfirmLogout()
        {
            if (MessageBox.Show("Anda ingin Logout?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Utility.GotoPage(this, "/View/LoginPage.xaml");
                SavedUser.Instance.LogOut();
            }
        }

        private void tileMenu1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, menu1.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        private void tileMenu2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, menu2.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        private void tileMenu3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, menu3.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        private void tileMenu4_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, menu4.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        private void tileMenu5_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, menu5.Page);
            PhoneApplicationService.Current.State.Clear();
        }

        private void buttonLogout_Click(object sender, EventArgs e)
        {
            ConfirmLogout();
        }

        private void textBlockUser_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ConfirmLogout();
        }

        private void buttonSetting_Click(object sender, EventArgs e)
        {
            Utility.GotoPage(this, "/View/SettingPage.xaml");
        }

        private void imageLogin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Utility.GotoPage(this, "/View/AccountPage.xaml");
        }

        private void CheckLoggedUser()
        {
            if (SavedUser.Instance.User != null)
            {
                textBlockUser.Text = "Hai, " + SavedUser.Instance.User.ToString();
            }
            else
            {
                textBlockUser.Text = "Logout";
            }
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Anda ingin Logout?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Utility.GotoPage(this, "/View/LoginPage.xaml");
                SavedUser.Instance.LogOut();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}